package cn.codesea.housework_user.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.MineChildRcvAdapter;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickUserListener;
import cn.codesea.housework_user.entity.User;
import cn.codesea.housework_user.net.ServiceGenerator;
import cn.codesea.housework_user.net.data.Result;
import cn.codesea.housework_user.net.service.UserService;
import cn.codesea.housework_user.utils.SPUtil;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Tab2MineChildFragment extends Fragment {

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.add_ll)
    LinearLayout add_ll;
    @BindView(R.id.modify_tv)
    TextView modify_tv;
    MineChildRcvAdapter adapter;

    LinearLayoutManager mLinearLayoutManager;

    @BindView(R.id.logout_tv)
    TextView logout_tv;
    @BindView(R.id.name_tv)
    TextView name_tv;

    List<User> olders = new ArrayList<>();
    User user;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab2_mine_child,container,false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // 在主页面切换fragment时，为什么每次都要重新执行过？
        init();
    }

    private void init(){
        initData();
        initView();
    }

    private void initData() {
        // 得到登陆用户的信息
        user = new Gson().fromJson(SPUtil.get(getActivity(),SPUtil.USER,""),User.class);

    }

    public void updateRecyclerView(){
        UserService userService = ServiceGenerator.createService(UserService.class);
        Call<Result> call = userService.getOlder(user.getUserid());
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        updateRecyclerView();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            updateRecyclerView();
        }
    }

    private void initView(){
        name_tv.setText(user.getName());

        adapter = new MineChildRcvAdapter(getActivity(),olders);
        adapter.setRecyclerItemClickListener(new OnRecyclerItemClickUserListener() {
            @Override
            public void onItemClick(int Position, List<User> list) {
//                Toast.makeText(getActivity(),Position+"查看:" +list.get(Position), Toast.LENGTH_SHORT).show();
                startActivity(new Intent(getActivity(),MineOldActivity.class).putExtra("older",list.get(Position)));
            }
        });
        mLinearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(adapter);
    }

    /**
     * 结果处理
     */
    public void resultProcess(Result result){
        Type type = new TypeToken<List<User>>(){}.getType();
        olders.clear();
        olders.addAll((List<User>)new Gson().fromJson(String.valueOf(result.getRes()),type));
        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.add_ll)
    public void add_ll(){
//        Toast.makeText(getActivity(),"新增", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(getActivity(),RegisterOldActivity.class).putExtra("type","child").putExtra("userid",user.getUserid()));
    }

    @OnClick(R.id.modify_tv)
    public void modify_tv(){
//        Toast.makeText(getActivity(),"编辑", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(getActivity(),RegisterChildActivity.class).putExtra("type","update"));

    }

    @OnClick(R.id.logout_tv)
    public void logout_tv(){
        // 测试
        SPUtil.put(getActivity(),SPUtil.LOG_STATE,"0");
        startActivity(new Intent(getActivity(),GuideActivity.class));
        getActivity().finish();
    }
}
